<template>
  <div class="LocationSearch">
    <div class="search_header">
      <div class="search_left">
        <img src="../../assets/type_03.jpg" alt>
        <input
          type="text"
          placeholder="输入地理位置"
          id="keyword"
          v-model="search"
          @change="LocationSearch($event,search)"
        >
      </div>
      <div class="search_right" @click.stop="remove">取消</div>
    </div>

   	<div id="allmap"></div>
    <div id="r-result"></div>
    <div class="lastest_searches">
      <div class="lastest_del" v-if="!this.info.length=='0'">
        <p>最近搜索</p>
        <img src="../../assets/del_03.jpg" alt="" @click.stop="delect">
      </div>
      <div class="lastest_del" v-else>
        <!-- <p>暂无搜索记录</p> -->
      </div>
      <div class="lastest_bottom">
        <ul>
          <li @click="zj(item)" v-for="(item,index) in info" :key="index" v-if="!item.content==''">{{item.content}}</li>
        </ul>
      </div>
    </div>
    <div class="lastest_searches">
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      search: "",
      userlocation: {
					lng: "",
					lat: ""
				},
      info: [],
      index: "",
      dz:''
    };
  },
  mounted() {
    console.log(JSON.parse(this.$route.query.newTime))
     setTimeout(()=>{
        let _this = this
			_this.$nextTick(function() {
				// 创建Map实例
				var map = new BMap.Map("allmap");
				// 初始化地图,设置中心点坐标，
				var point = new BMap.Point(116.4, 39.9); // 创建点坐标，获得公司的经纬度坐标
				var gc = new BMap.Geocoder();
				map.centerAndZoom(point, 15);
				map.enableScrollWheelZoom();
				var ac = new BMap.Autocomplete( //建立一个自动完成的对象
					{
						"input": "keyword",
						"location": map
					})
				gc.getLocation(point, function(rs) {
					var addComp = rs.addressComponents;
				});
				let myValue
        ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
					let _value = e.item.value;
          myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
          _this.search = myValue
          console.log(_this.search)
					_this.baidu = myValue
					setPlace();
				});

				function setPlace() {
					map.clearOverlays(); //清除地图上所有覆盖物
					function myFun() {
            _this.userlocation = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
              // mui.toast("当前位置"+_this.search) 
              //     _this.$router.push({
              //             path: "/Venue_management",
              //             query: {
              //                lng: local.getResults().getPoi(0).point.lng,
              //                lat: local.getResults().getPoi(0).point.lat,
              //                dz:_this.search
              //             }
              //           })
                _this.$message({
                    message: "当前位置"+_this.search,
                    type: 'warning'
                    });
                     _this.$router.push({
                          path: "/Venue_management",
                          query: {
                             lng: local.getResults().getPoi(0).point.lng,
                             lat: local.getResults().getPoi(0).point.lat,
                             dz:_this.search,
                             mc:_this.$route.query.mc,
                             newTime:_this.$route.query.newTime,
                             dh:_this.$route.query.dh,
                             tag:_this.$route.query.tag,
                             value1:_this.$route.query.value1,
                             image:_this.$route.query.image,
                             video:_this.$route.query.video        
                          }
                        })
						map.centerAndZoom(_this.userlocation, 18);
						map.addOverlay(new BMap.Marker(_this.userlocation)); //添加标注
					}

					var local = new BMap.LocalSearch(map, { //智能搜索
						onSearchComplete: myFun
					});
					local.search(myValue);
						
					//测试输出坐标（指的是输入框最后确定地点的经纬度）
					map.addEventListener("click", function(e) {
            console.log(e)
            //经度
						console.log(_this.userlocation.lng);
						//维度
						console.log(_this.userlocation.lat);
          })
				}

			})
    
     },1000)
  },
  methods: {
    
    remove() {
      this.$router.push({
        path: "/Venue_management",query: {
                           
                             mc:this.$route.query.mc,
                             newTime:this.$route.query.newTime,
                             dh:this.$route.query.dh,
                             tag:this.$route.query.tag,
                             value1:this.$route.query.value1,
                             image:this.$route.query.image,
                             video:this.$route.query.video         
                          }
      });
    },
    LocationSearch() {
    },
    delect() {
      let _this = this;
      var SESSION_ID = JSON.parse(window.localStorage.getItem("sessiond"));
      let qs = require("qs");
      let obj = {
        userid: SESSION_ID.user.id,
        type: "1"
      };
      _this.$axios
        .post(
          "/yujiaextend/yogagym/deleteUserSearchByUserid",
          qs.stringify(obj),
          {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            }
          }
        )
        .then(response => {
          if (response.data.code == "1") {
            window.localStorage.removeItem('mrjd')
            let obj3 = {
              userid: SESSION_ID.user.id,
              type: "1"
            };
            _this.$axios
              .post(
                "/yujiaextend/yogagym/findAllUserSearchByUserIdAndType",
                qs.stringify(obj3),
                {
                  headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  }
                }
              )
              .then(response => {
                _this.info = response.data.data;
              })
              .catch(error => {
                console.log(error);
              });
          } else if (response.data.code == "-1") {
            mui.toast(response.data.message);
          } else if (response.data.code == "-2") {
            mui.toast(response.data.message);
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>
<style scoped>
/* 搜索头部   */
.search_header {
  width: 92%;
  margin: 1rem /* 16/16 */ auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.search_header .search_left {
  width: 86%;
  height: 1.875rem /* 30/16 */;
  background: #eeeeee;
  display: flex;
  align-items: center;
  border-radius: 1.5625rem /* 25/16 */;
}

.search_header .search_left img {
  width: 0.8125rem /* 13/16 */;
  height: 0.9375rem /* 15/16 */;
  display: inline-block;
  margin-left: 1rem /* 16/16 */;
}
.search_header .search_left input {
  width: 86%;
  height: 1.875rem /* 30/16 */;
  padding: 0;
  padding-left: 0.3125rem /* 5/16 */;
  border: none;
  background: #eeeeee;
  outline: none;
  font-size: 0.8125rem /* 13/16 */;
  color: #aaaaaa;
}
.search_header .search_left input::placeholder {
  font-size: 0.8125rem /* 13/16 */;
  color: #aaaaaa;
}
.search_header .search_right {
  font-size: 0.9375rem /* 15/16 */;
  color: #000000;
  font-family: "微软雅黑";
}
/* 搜索头部结束   */

/* 最近搜索 */

.lastest_searches {
  width: 92%;
  margin: 0 auto;
  float: left;
  margin-left: 4%;
  z-index: 999999999999999999999999
}
.lastest_searches .lastest_del {
  width: 100%;
  float: left;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem /* 20/16 */ 0;
}
.lastest_searches .lastest_del p {
  font-size: 0.8125rem /* 13/16 */;
  color: #606060;
  font-weight: bold;
}
.lastest_searches .lastest_del img {
  width: 0.9375rem /* 15/16 */;
  height: 0.90625rem /* 14.5/16 */;
}
.lastest_searches .lastest_bottom {
  width: 100%;
  float: left;
}
.lastest_searches .lastest_bottom ul {
  width: 100%;
  float: left;
}

.lastest_searches .lastest_bottom ul li {
  background: #eeeeee;
  float: left;
  padding: 0.375rem /* 6/16 */ 0.625rem /* 10/16 */;
  margin-right: 0.4375rem /* 7/16 */;
  margin-bottom: 0.625rem /* 10/16 */;
  font-size: 0.875rem /* 14/16 */;
  color: #000000;
  line-height: 0.875rem /* 14/16 */;
  border-radius: 0.9375rem /* 15/16 */;
}
/* 最近搜索结束 */
#allmap{
		width: 100%;
		float: left;
		background: #fff!important;
		margin-top: .9375rem /* 15/16 */;
    position: absolute;
    top: 3.75rem /* 60/16 */;
	}
	

</style>
